package picobot.tests.students;

import java.io.File;

import picobot.interfaces.core.IMap;
import picobot.interfaces.core.IMapBuilder;
import picobot.interfaces.core.IPicobot;
import picobot.interfaces.core.ISimulator;
import picobot.simulation.Simulator;
import picobot.tests.Bootstrap;
import junit.framework.TestCase;

/**
 * This class tests all the methods of Simulator
 */
public class TestSimulator extends TestCase {

	// toEmptyRandomCell will not be tested, the new position will just be
	// printed out and can be checked manual

	/**
	 * 'tests' the method toEmptyRandomCell
	 */
	public void testToEmptyRandomCell() {
		Simulator sim = new Simulator();

		// creating and loading the map
		IMapBuilder mb = Bootstrap.f().createMapBuilder();
		mb.createEmptySquareMap(5);
		IMap map = mb.getCurrentMap();

		sim.loadMap(map);

		// creating and loading the picobot
		IPicobot bot = Bootstrap.f().createPicobot();
		bot.loadRules(Bootstrap.f().createRuleBuilder()
				.parseRules("1 NEWS -> N 1"));
		sim.loadPicobot(bot);

		sim.toEmptyRandomCell();
		System.out.println(sim.getPicobot().getXCoordinate() + ","
				+ sim.getPicobot().getYCoordinate());

		// with a high probability the position is now different
		sim.toEmptyRandomCell();
		System.out.println(sim.getPicobot().getXCoordinate() + ","
				+ sim.getPicobot().getYCoordinate());
	}

	/**
	 * tests both the methods load- and getPicobot
	 */
	public void testLoadGetPicobot() {
		ISimulator sim = Bootstrap.f().createSimulator();
		IPicobot bot = Bootstrap.f().createPicobot();

		bot.loadRules(Bootstrap.f().createRuleBuilder()
				.parseRules("1 NEWS -> N 1"));
		sim.loadPicobot(bot);

		assertEquals(bot, sim.getPicobot());

	}

	/**
	 * tests both the methods load- and getMap
	 */
	public void testLoadGetMap() {
		ISimulator sim = Bootstrap.f().createSimulator();
		IMapBuilder mb = Bootstrap.f().createMapBuilder();

		mb.createEmptySquareMap(10);
		mb.placeWall(1, 1);
		IMap map = mb.getCurrentMap();

		sim.loadMap(map);

		assertEquals(map, sim.getMap());
	}

	/**
	 * tests the step method
	 * 
	 */
	public void testStep() {
		ISimulator sim = Bootstrap.f().createSimulator();
		IMapBuilder mb = Bootstrap.f().createMapBuilder();

		mb.createEmptySquareMap(10);
		mb.placeWall(1, 1);
		IMap map = mb.getCurrentMap();

		IPicobot bot = Bootstrap.f().createPicobot();
		bot.loadRules(Bootstrap.f().createRuleBuilder()
				.parseRules("0 xxWS -> N 1"));
		;

		sim.loadMap(map);
		sim.loadPicobot(bot);
		bot.setInitialPosition(1, 1);

		sim.step();
		assertEquals(2, sim.getPicobot().getYCoordinate());
	}

	// getTraversedCells is tested in PublicTestSimulator

	/**
	 * tests the method isMissionCompleted
	 */
	public void testIsMissionCompleted() {
		ISimulator sim = Bootstrap.f().createSimulator();
		IMapBuilder mb = Bootstrap.f().createMapBuilder();

		mb.parseMap(new File("maps/map1-mission1-empty.map"));
		IMap map = mb.getCurrentMap();

		IPicobot bot = Bootstrap.f().createPicobot();
		bot.loadRules(Bootstrap.f().createRuleBuilder()
				.parseRules(new File("rules/default-rule.rule")));

		sim.loadMap(map);
		sim.loadPicobot(bot);
		bot.setInitialPosition(1, 1);

		// clear all cells but one, mission should not be completed yet
		sim.run(528);
		assertFalse(sim.isMissionCompleted());
		// clear the last cell, mission will be completed
		sim.step();
		assertTrue(sim.isMissionCompleted());
	}

	/**
	 * tests the method run and reset
	 */
	public void testRunReset() {
		ISimulator sim = Bootstrap.f().createSimulator();
		IMapBuilder mb = Bootstrap.f().createMapBuilder();

		mb.parseMap(new File("maps/map1-mission1-empty.map"));
		IMap map = mb.getCurrentMap();

		IPicobot bot = Bootstrap.f().createPicobot();
		bot.loadRules(Bootstrap.f().createRuleBuilder()
				.parseRules(new File("rules/default-rule.rule")));

		sim.loadMap(map);
		sim.loadPicobot(bot);
		bot.setInitialPosition(1, 1);

		sim.run(10);
		assertEquals(bot.getXCoordinate(), 11);
		assertEquals(bot.getYCoordinate(), 1);

		sim.reset();
		assertTrue(sim.getTraversedCells().isEmpty());
	}

}
